package com.zbkj.admin.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.common.model.user.User;
import com.zbkj.service.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Auther:
 * @Date: 2024/9/16 15:47
 * @Description:
 */
@Slf4j
@Component
public class UserJobTask {

    @Autowired
    private UserService userService;

    /**
     * 用户年卡失效处理
     * @return
     */
    @XxlJob("userCardExpireTask")
    public ReturnT userCardExpireTask() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
        wrapper.eq(User::getDeleteFlag,0)
                .gt(User::getCardId,0)
                .lt(User::getCardExpire, new Date());
        List<User> taskList = userService.list(wrapper);
        for(User user : taskList) {
            try {
                userService.userCardExpireHandle(user);
            } catch (Exception e) {
                log.error("userCardExpireTask-userId:{}-cardId:{}-年卡失效处理error:{}",user.getId(),user.getCardId(),e.getMessage());
            }
        }
        log.info("userCardExpireTask-size:{}",taskList.size());
        return ReturnT.SUCCESS;
    }

}
